package scau.redi.social_platform.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文件相关操作工具类
 * @author zhangshaolong
 */
@Component
public class FileUtil {

    /**
     * 本地文件上传的路径
     */
    public static String filePath;

    /**
     * 服务器的ip地址
     * 或者是域名地址
     */
    public static String serverIp;


    /**
     * 上传单个文件
     * @param file 要上传的文件
     * @return
     */
    public static String uploadFile(MultipartFile file) {
        if (Objects.isNull(file)) {
            return "";
        }
        // 文件夹不存在则创建文件夹
        String uploadDirectorPath = getUploadDirectorPath();
        File file1 = new File(uploadDirectorPath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        String originalFilename = file.getOriginalFilename();
        String newFileName = getNewFileName(originalFilename);
        file1 = new File(file1, newFileName);
        try {
            // 保存文件内容
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 访问能够通过浏览器访问的路径
        return serverIp + getTodayPath() + "/" + newFileName;
    }

    /**
     * 上传多个文件
     * @param files
     * @return
     */
    public static List<String> uploadFile(List<MultipartFile> files) {
        if (Objects.isNull(files) || files.isEmpty()) {
            return null;
        }

        List<String> filePathList = new ArrayList<>();
        for (MultipartFile file : files) {
            filePathList.add(uploadFile(file));
        }
        return filePathList;
    }

    /**
     * 上传多个文件
     * @param files
     * @return
     */
    public static List<String> uploadFile(MultipartFile[] files) {
        if (Objects.isNull(files) || files.length == 0) {
            return null;
        }

        List<String> filePathList = new ArrayList<>();
        for (MultipartFile file : files) {
            filePathList.add(uploadFile(file));
        }
        return filePathList;
    }

    /**
     * 删除文件
     */
    public static void deleteFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 删除多个文件
     * @param filePathList
     */
    public static void deleteFile(List<String> filePathList) {
        if (Objects.isNull(filePathList) || filePathList.isEmpty()) {
            return;
        }

        for (String s : filePathList) {
            deleteFile(s);
        }
    }

    /**
     * 获取当天的日期作为第二级文件夹
     * 年-月-日
     */
    public static String getTodayPath() {
        return DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");
    }

    /**
     * 获取文件上传的文件夹名称
     * @return 根文件夹/2021-4-26/
     */
    private static String getUploadDirectorPath() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(filePath).append(getTodayPath()).append("/");
        return stringBuilder.toString();
    }

    /**
     * 生成新的文件名
     * @param fileName
     * @return
     */
    private static String getNewFileName(String fileName) {
        if (Objects.isNull(fileName)) {
            return "";
        }

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UUIDUtil.getUUID()).append(getSuffix(fileName));
        return stringBuilder.toString();
    }

    /**
     * 获取文件名的后缀
     * @param fileName
     * @return
     */
    private static String getSuffix(String fileName) {
        if (Objects.isNull(fileName)) {
            return "";
        }
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * 获取视频在服务器中的位置
     * 要对可以在浏览器中访问的路径进行切割
     * 同时在nginx中配置了静态资源的访问
     * @param uploadPath
     * @return
     */
    public static String getLocalPath(String uploadPath) {
        String[] split = uploadPath.split(serverIp);
        return filePath + split[1];
    }

    /**
     * 获取文件的浏览器访问地址
     * @param imageLocalPath
     * @return
     */
    public static String getUrl(String imageLocalPath) {
        String[] split = imageLocalPath.split(filePath);
        return serverIp + split[1];
    }

    /**
     * 从配置文件中注入文件url前缀
     * @param filePath
     */
    @Value("${web.upload-path}")
    public void setFilePath(String filePath) {
        FileUtil.filePath = filePath;
    }
    /**
     * 从配置文件中注入文件保存的server ip前缀
     * @param serverIp
     */
    @Value("${web.server-ip}")
    public void setServerIp(String serverIp) {
        FileUtil.serverIp = serverIp;
    }

    public static void main(String[] args) {
        String videoPath = getLocalPath("http://120.79.57.23/chicken_video/2021-09-07/8b59e909ccad460eb75f66ef061e5fd8.mp4");
        System.out.println("videoPath = " + videoPath);
        String url = getUrl(videoPath);
        System.out.println("url = " + url);

        DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");

        String uploadDirectorPath = getUploadDirectorPath();
        System.out.println("uploadDirectorPath = " + uploadDirectorPath);
    }

}
